﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utilities;
using Telerik.Web.UI;
using Casanova.Web;
using Domain.DAL;
using Domain.Models;
using System.Data;

namespace WebApp.ReportsAndStatistics
{
    public partial class ReportByProductTypesDetails : System.Web.UI.Page
    {
        #region Private Properties

        public int ProductTypeID
        {
            get
            {
                if (this.ViewState["ProductTypeID"] != null)
                {
                    return Convert.ToInt32(this.ViewState["ProductTypeID"]);
                }
                return MyConfig.NullNumber;
            }
            set
            {
                this.ViewState["ProductTypeID"] = value.ToString();
            }
        }

        #endregion

        #region RadGrid's Event Handlers

        protected void grdResult_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            try
            {
                Utility.TranslateGridPagerItem(e);
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        protected void grdResult_ItemDataBound(object sender, GridItemEventArgs e)
        {
            try
            {
                if (e.Item.ItemType == GridItemType.AlternatingItem || e.Item.ItemType == GridItemType.Item)
                {
                    GridEditableItem editableItem = (GridEditableItem)e.Item;
                    if (editableItem != null)
                    {
                        //Set navigate url for hyperlink fullname
                        string repairingTicketID = editableItem["RepairingTicketID"].Text.Trim();
                        if (!string.IsNullOrEmpty(repairingTicketID))
                        {
                            HyperLink hplFullName = (HyperLink)editableItem["FullName"].FindControl("hplFullName");
                            if (hplFullName != null)
                            {
                                //string preUrl = string.Format("{0}?productTypeID={1}&reportDateFrom={2}&reportDateTo={3}&preUrl={4}", ResolveUrl("~/ReportsAndStatistics/ReportByProductTypesDetails.aspx"), QueryStringUtility.GetString("productTypeID", MyConfig.NullNumber.ToString()), QueryStringUtility.GetString("reportDateFrom"), QueryStringUtility.GetString("preUrl"));
                                hplFullName.NavigateUrl = String.Format("{0}?RepairingTicketID={1}&isReportByProductType=1&productTypeID={2}&reportDateFrom={3}&reportDateTo={4}&preUrl={5}", this.ResolveUrl("~/RepairingTickets/TicketDetails.aspx"), repairingTicketID, QueryStringUtility.GetString("productTypeID", MyConfig.NullNumber.ToString()), QueryStringUtility.GetString("reportDateFrom"), QueryStringUtility.GetString("reportDateTo"), QueryStringUtility.GetString("preUrl"));
                            }
                        }

                        //format cost
                        if (!string.IsNullOrEmpty(editableItem["Cost"].Text.Trim()) && editableItem["Cost"].Text.Trim() != "&nbsp;")
                        {
                            editableItem["Cost"].Text = Utility.FormatNumber(editableItem["Cost"].Text, MyConfig.CurrencyFormat, TypeCode.Decimal);
                        }

                        //Visible check warranty
                        string checkWarranty = editableItem["IsValidWarranty"].Text.Trim();
                        string visibleCheckWarranty = editableItem["VisibleCheckWarranty"].Text.Trim();

                        if (!string.IsNullOrEmpty(visibleCheckWarranty) && !string.IsNullOrWhiteSpace(visibleCheckWarranty))
                        {
                            System.Web.UI.WebControls.Image imgCheckWarranty = (System.Web.UI.WebControls.Image)editableItem["ColCheckWarranty"].FindControl("imgCheckWarranty");
                            if (imgCheckWarranty != null)
                            {
                                imgCheckWarranty.Visible = Convert.ToBoolean(visibleCheckWarranty);

                                if (imgCheckWarranty.Visible)
                                {
                                    if (!string.IsNullOrEmpty(checkWarranty) && !string.IsNullOrWhiteSpace(checkWarranty) && checkWarranty != "&nbsp;")
                                    {
                                        if (checkWarranty == "1")
                                        {
                                            imgCheckWarranty.ImageUrl = this.ResolveUrl("~/images/ValidWarranty.gif");
                                        }
                                        else
                                        {
                                            imgCheckWarranty.ImageUrl = this.ResolveUrl("~/images/InvalidWarranty.gif");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        protected void grdResult_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            try
            {
                BindGrid();
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        #endregion

        #region Event Handlers

        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {
                (Master as ReportsAndStatisticsPages).SelectedTab = "reportsAndStatistics";
                if (!IsPostBack)
                {
                    LoadForm();
                }
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(this.ResolveUrl(QueryStringUtility.GetString("preUrl", "~/ReportsAndStatistics/ReportByProductTypes.aspx")));
        }

        #endregion

        #region Private Methods

        private void BindGrid()
        {
            try
            {
                DataSet ds = new DataSet();

                string reportDateFrom = lblReportDateFrom.Text;
                string reportDateTo = lblReportDateTo.Text;

                ReportsAndStatisticsDAO rsDAO = ReportsAndStatisticsDAO.GetInstance();
                if (rsDAO != null)
                {
                    ds = rsDAO.ReportByProductTypeDetails(ProductTypeID.ToString(), reportDateFrom, reportDateTo);
                    if (ds != null && ds.Tables.Count > 1)
                    {
                        if (ds.Tables[ds.Tables.Count - 1].Rows.Count > 0)
                        {
                            if (ds.Tables[ds.Tables.Count - 1].Rows[0]["TotalQuantity"] != null && ds.Tables[ds.Tables.Count - 1].Rows[0]["TotalQuantity"] != DBNull.Value)
                            {
                                lblTotalQuantity.Text = Utility.FormatNumber(ds.Tables[ds.Tables.Count - 1].Rows[0]["TotalQuantity"].ToString(), MyConfig.NumberFormat, TypeCode.Int64);
                            }
                            if (ds.Tables[ds.Tables.Count - 1].Rows[0]["TotalCost"] != null && ds.Tables[ds.Tables.Count - 1].Rows[0]["TotalCost"] != DBNull.Value)
                            {
                                lblTotalCost.Text = Utility.FormatNumber(ds.Tables[ds.Tables.Count - 1].Rows[0]["TotalCost"].ToString(), MyConfig.CurrencyFormat, TypeCode.Decimal);
                            }

                        }
                        grdResult.DataSource = ds.Tables[0];
                    }

                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void LoadForm()
        {
            try
            {
                (Master as ReportsAndStatisticsPages).PageTitle = Resources.Strings.ReportByProductTypes;

                ProductTypeID = QueryStringUtility.GetInt32("productTypeID", MyConfig.NullNumber);
                lblReportDateFrom.Text = QueryStringUtility.GetString("reportDateFrom", string.Empty);
                lblReportDateTo.Text = QueryStringUtility.GetString("reportDateTo", string.Empty);

                ProductTypeDAO ptDAO = ProductTypeDAO.GetInstance();
                if (ptDAO != null)
                {
                    ProductType pt = new ProductType();

                    pt = ptDAO.Get(QueryStringUtility.GetInt32("productTypeID", MyConfig.NullNumber));
                    if (pt != null && !string.IsNullOrEmpty(pt.ProductTypeName))
                    {
                        lblProductTypeName.Text = pt.ProductTypeName;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion

    }
}